package leetcode;

import org.junit.Test;

import java.util.HashMap;

/**
 * @author cino
 * justCode
 */
public class leetcode242 {

    @Test
    public void test(){
        System.out.println(isAnagram1("aacc", "ccac"));
    }
    public boolean isAnagram(String s, String t) {
        char[] c1 = s.toCharArray();
        char[] c2 = t.toCharArray();
        int l1 = s.length();
        int l2 = t.length();
        HashMap<Character, Integer> map = new HashMap<>();
        if (l1 != l2){
            return false;
        }
        for (int i = 0 ;i<l1;i++){
            if (!map.containsKey(c1[i])){
                map.put(c1[i],1);
            }else {
                map.put(c1[i],map.get(c1[i])+1);
            }
        }
        for (int i =0;i<l2;i++){
            if (map.containsKey(c2[i])){
                map.put(c2[i],map.get(c2[i])-1);
            }else {
                return false;
            }
        }
        int sum = 0;
        for (Integer value : map.values()) {
            sum += value;
        }
        if (sum!=0 || sum<0){
            return false;
        }else {
            return true;
        }
    }

    public boolean isAnagram1(String s, String t) {
        if(s.length() != t.length())
            return false;
        int[] alpha = new int[26];
        for(int i = 0; i< s.length(); i++) {
            alpha[s.charAt(i) - 'a'] ++;
            alpha[t.charAt(i) - 'a'] --;
        }
        for(int i=0;i<26;i++)
            if(alpha[i] != 0)
                return false;
        return true;
    }

}
